const app = getApp();
Component({
    properties: {

    },
    data: {
        searchValue: "",
        hadReceiveList: [],
        isLastPage: false,
        current: 1,
        size: 10,
        qualityMap: {
            1: "精品",
            2: "次品",
            3: "统货",
        },
    },

    lifetimes: {
        attached: function () {
            // 在组件实例进入页面节点树时执行
            this.getData()
        },
        detached: function () {
            // 在组件实例被从页面节点树移除时执行
        },
    },

    pageLifetimes: {
        show: function () {
            this.onPullDownRefresh()
        },
    },

    /**
     * 组件的方法列表
     */
    methods: {
        onPullDownRefresh() {
            this.setData({
                current: 1,
                searchValue: "",
                hadReceiveList: [],
                isLastPage: false
            })
            this.getData()
        },
        onReachBottom() {
            let {
                current,
                isLastPage
            } = this.data
            if (isLastPage) {
                return
            }
            this.setData({
                current: (Number(current) + 1)
            })
            this.getData()
        },
        onSearch() {
            this.setData({
                current: 1,
                hadReceiveList: [],
                isLastPage: false
            })
            this.getData()
        },
        toPurchaserDetail(e) {
            let item = e.currentTarget.dataset.item
            let title = `${item.name} ${item.mobile}`
            wx.navigateTo({
                url: `/pages/payment/had-receive/index?id=${item.userId}&title=${title}`,
            })
        },
        getData() {
            const {
                role,
                current,
                searchValue,
                size
            } = this.data;
            let params = {
                settleIs: true,
                current,
                size,
                keyword: searchValue,
                companyId: app.globalData.userInfo.companyData.companyId,
                enterStatus: 4
            }
            wx.showLoading({
                title: "正在请求数据中"
            })
            wx.$apis.getOutStockSettlementPage(params).then(res => {
                wx.stopPullDownRefresh();
                let oldHadReceiveList = this.data.hadReceiveList;
                let hadReceiveList = res.data && res.data.records.map(item => {
                    item.settleAccountPrice = item.settleAccount && wx.$getYuan(item.settleAccount)
                    return item
                })
                if (hadReceiveList && hadReceiveList.length) {
                    let newHadReceiveList = oldHadReceiveList.concat(hadReceiveList)
                    this.setData({
                        hadReceiveList: newHadReceiveList
                    })
                    if (hadReceiveList.length < size) {
                        this.setData({
                            isLastPage: true
                        })
                    }
                } else {
                    this.setData({
                        isLastPage: true
                    })
                }
            })
        }
    }
})